package com.instagram.instadownloader.bookmark;

import java.io.File;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.vine.vinedownloader.R;
import com.instagram.instadownloader.fetcher.FetcherData;
import com.instagram.instadownloader.fetcher.InstaVideoFetcher;
import com.instagram.instadownloader.fetcher.InstaVideoFetcher.OnFetcherListener;
import com.instagram.instadownloader.history.HistoryObject;
import com.instagram.instadownloader.utils.ClipboardUtil;
import com.instagram.instadownloader.utils.Launcher;
import com.instagram.instadownloader.utils.SharedPrefs;
import com.instagram.instadownloader.utils.UniversalImageLoader;

// TODO: Auto-generated Javadoc
/**
 * The Class BookmarkViewBlock.
 */
public class BookmarkViewBlock extends LinearLayout implements OnClickListener {

	/** The m image view. */
	private ImageView imgShowPic;

	/** The m image path. */
	private String mImagePath;

	/** The m share url. */
	private String mShareUrl;

	/** The m view. */
	private View mView;

	private int mObject = -1;

	public void setPositionBookmark(int pos) {
		this.mObject = pos;
		String url = BookmarksManager.getInstance().getBookmarkByPosition(mObject).getUrl();
		if (url != null && !url.isEmpty()) {
			startFetchingUrl(url);
		}
	}

	/**
	 * Instantiates a new bookmark view block.
	 * 
	 * @param context
	 *            the context
	 * @param attrs
	 *            the attrs
	 * @param defStyle
	 *            the def style
	 */
	public BookmarkViewBlock(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		createView(context);
	}

	/**
	 * Instantiates a new bookmark view block.
	 * 
	 * @param context
	 *            the context
	 * @param attrs
	 *            the attrs
	 */
	public BookmarkViewBlock(Context context, AttributeSet attrs) {
		super(context, attrs);
		createView(context);
	}

	/**
	 * Instantiates a new bookmark view block.
	 * 
	 * @param context
	 *            the context
	 */
	public BookmarkViewBlock(Context context) {
		super(context);
		createView(context);
	}

	/**
	 * Creates the view.
	 */
	public void createView(Context context) {
		mView = LayoutInflater.from(context).inflate(R.layout.view_bookmark_block, null);
		imgShowPic = ((ImageView) this.mView.findViewById(R.id.imgShow));
		imgShowPic.setOnClickListener(this);
		((ImageView) this.mView.findViewById(R.id.btnInstagram)).setOnClickListener(this);
		((ImageView) this.mView.findViewById(R.id.btnCopi)).setOnClickListener(this);
		((ImageView) this.mView.findViewById(R.id.btnShare)).setOnClickListener(this);
		((ImageView) this.mView.findViewById(R.id.btnDelete)).setOnClickListener(this);

		addView(mView);
	}

	public void putDataToBox(HistoryObject pObj) {

	}

	private void shareInstagramImage(Context paramContext, String absolutePath, String url) {
		if (("".equals(absolutePath)) || ("".equals(url))) {
			return;
		}
		Intent localIntent = new Intent("android.intent.action.SEND");
		localIntent.setType("image/jpeg");
		localIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
		localIntent.putExtra("android.intent.extra.SUBJECT",
				"Let's check out this interesting Instagram post");
		localIntent.putExtra("android.intent.extra.TEXT", url + " #InstaDownloader");
		if (absolutePath != null && !absolutePath.isEmpty()) {
			localIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(new File(absolutePath)));
		}
		paramContext.startActivity(Intent.createChooser(localIntent, "Share image via"));
	}

	@SuppressLint({ "NewApi" })
	protected void copyToClipboard(Context paramContext, String string) {
		// if (Build.VERSION.SDK_INT < 11)
		// ((android.text.ClipboardManager)paramContext.getSystemService("clipboard")).setText(string);
		// else{
		// ((android.content.ClipboardManager)paramContext.getSystemService("clipboard")).setPrimaryClip(ClipData.newPlainText("share url",
		// string));
		// }
		ClipboardUtil.putText(paramContext, string);
		Toast.makeText(paramContext, "Copied\n" + string, 0).show();
	}

	public void setImage() {
		if (!SharedPrefs.getInstance().getDefaultPath().equals("")) {

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgShow:
			break;
		case R.id.btnDelete:
			if (this.mObject != -1) {
				BookmarksManager.getInstance().removeBookmark(this.mObject);
			}
			this.removeAllViews();
			// SharedPrefs.getInstance().setBookmarkUrlPath("");
			// SharedPrefs.getInstance().setDefaultPath("");
			// BookmarkViewBlock.this.setVisibility(View.GONE);
			break;
		case R.id.btnInstagram:
			Launcher.openWebView(getContext(), SharedPrefs.getInstance().getBookMarkUrl());
			break;
		case R.id.btnCopi:
			if (!SharedPrefs.getInstance().getBookMarkUrl().equals("")) {
				copyToClipboard(getContext(), BookmarksManager.getInstance().getBookmarks().get(this.mObject)
						.getUrl());
			}
			break;
		case R.id.btnShare:
			BookmarkObject obj = BookmarksManager.getInstance().getBookmarks().get(this.mObject);
			shareInstagramImage(getContext(), obj.getAbsolutePath(), obj.getUrl());
			break;
		default:
			break;
		}
	}

	public void startFetchingUrl(final String string) {
		InstaVideoFetcher localInstaVideoFetcher = new InstaVideoFetcher();
		String uRLTobeFetched = localInstaVideoFetcher.validate(string);
		localInstaVideoFetcher.fetch(uRLTobeFetched, new OnFetcherListener() {

			@Override
			public void onFetcherFail(String message) {
			}

			@Override
			public void onFetcherComplete(FetcherData data) {
				UniversalImageLoader.getInstance().displayImage(data.getImageUrl(), imgShowPic);
			}
		});
	}

}
